﻿// <copyright file="InGameMenu.cs" company="ThorIndustry">Copyright (c) ThorIndustry. All rights reserved.</copyright>

using System.Collections.Generic;

public class InGameMenu : Menu
{
    private IViewService viewService;
    
    public override void Initialize(IGuiController guiService)
    {
        base.Initialize(guiService);

        this.viewService = Services.GetService<IViewService>();

        this.InitializeMenu();
    }

    private void InitializeMenu()
    {
        float centerY = UnityEngine.Screen.height / 2;

        float x = 0.5f;

        // We need 3 buttons.
        // First, the help one.
        UnityEngine.GameObject buttonObject = UnityEngine.GameObject.Instantiate(this.ButtonPrefab) as UnityEngine.GameObject;
        Button currentButton = buttonObject.GetComponent<Button>();
        this.buttons.Add(currentButton);
        currentButton.IsVisible = this.IsVisible;
        currentButton.Clicked += this.ButtonHelp_Clicked;

        // Place the button.
        UnityEngine.Transform buttonTransform = currentButton.transform;
        buttonTransform.parent = this.transform;
        currentButton.GuiText.text = "Need some Help!";

        UnityEngine.Rect rect = GuiHelper.FormatGuiTextArea(currentButton.GuiText, 300);
        rect.height = 50 * GuiHelper.GetScreenSizeRatio();
        rect.width = rect.width + (10 * GuiHelper.GetScreenSizeRatio());
        rect.x = -rect.width / 2;
        rect.y = -rect.height / 2;
        currentButton.GuiTexture.pixelInset = rect;

        buttonTransform.localPosition = new UnityEngine.Vector3(x, (centerY + (60 * GuiHelper.GetScreenSizeRatio())) / UnityEngine.Screen.height, 0);

        currentButton.GuiText.pixelOffset = new UnityEngine.Vector2(0, 0);

        // Then, the Back to main menu one.
        buttonObject = UnityEngine.GameObject.Instantiate(this.ButtonPrefab) as UnityEngine.GameObject;
        currentButton = buttonObject.GetComponent<Button>();

        this.buttons.Add(currentButton);
        currentButton.IsVisible = this.IsVisible;
        currentButton.Clicked += this.ButtonBackToMain_Clicked;

        // Place the button.
        buttonTransform = currentButton.transform;
        buttonTransform.parent = this.transform;

        currentButton.GuiText.text = "Choose another puzzle";

        rect = GuiHelper.FormatGuiTextArea(currentButton.GuiText, 300);
        rect.height = 50 * GuiHelper.GetScreenSizeRatio();
        rect.width = rect.width + (10 * GuiHelper.GetScreenSizeRatio());
        rect.x = -rect.width / 2;
        rect.y = -rect.height / 2;
        currentButton.GuiTexture.pixelInset = rect;
        buttonTransform.localPosition = new UnityEngine.Vector3(x, (centerY - (60* GuiHelper.GetScreenSizeRatio())) / UnityEngine.Screen.height, 0);

        currentButton.GuiText.pixelOffset = new UnityEngine.Vector2(0, 0);

        // Finally, the back to game one.
        buttonObject = UnityEngine.GameObject.Instantiate(this.ButtonPrefab) as UnityEngine.GameObject;
        currentButton = buttonObject.GetComponent<Button>();
        currentButton.transform.parent = this.transform;

        this.buttons.Add(currentButton);
        currentButton.IsVisible = this.IsVisible;
        currentButton.Clicked += this.ButtonBackToGame_Clicked;

        // Place the button.
        buttonTransform = currentButton.transform;
        buttonTransform.parent = this.transform;

        currentButton.GuiText.text = "Back";

        rect = GuiHelper.FormatGuiTextArea(currentButton.GuiText, 300);
        rect.height = 50 * GuiHelper.GetScreenSizeRatio();
        rect.width = UnityEngine.Mathf.Max((75 * GuiHelper.GetScreenSizeRatio()), rect.width + (10 * GuiHelper.GetScreenSizeRatio()));
        rect.x = -rect.width / 2;
        rect.y = -rect.height / 2;
        currentButton.GuiTexture.pixelInset = rect;
        buttonTransform.localPosition = new UnityEngine.Vector3(x, 0.1f, 0);

        currentButton.GuiText.pixelOffset = new UnityEngine.Vector2(0, 0);
    }

    private void ButtonBackToMain_Clicked(object sender, System.EventArgs args)
    {
        this.GuiController.BackMenu();

        this.viewService.Activate(typeof(OutGameView));
    }

    private void ButtonBackToGame_Clicked(object sender, System.EventArgs args)
    {
        this.GuiController.BackMenu();
    }

    private void ButtonHelp_Clicked(object sender, System.EventArgs args)
    {
        this.GuiController.BackMenu();
    }
}
